<template>
    <div class="login">
        <!-- 登录头部 -->
        <header class="header">
            <div class="logo">
                <div class="sys-logo"><SvgIcons class="vite" name="vite" /></div>
                <div class="sys-name">数据管理系统</div>
            </div>
            <div class="sys-time">{{ currentTime }}</div>
        </header>
        <!-- 登录内容区 -->
        <div class="container">
            <Transition appear enter-active-class="animate__animated animate__bounceInLeft">
                <div class="login-contain">
                    <div class="contain-left">
                        <div class="left-box">
                            <div class="left-text">WELCOME TO</div>
                            <div class="left-system">数据管理系统</div>
                        </div>
                    </div>
                    <div class="contain-right">
                        <div class="login-box">
                            <div class="login-title">
                                <SvgIcons class="login-vite" name="vite" />
                                <div class="login-name">LOG IN TO YOUR ACCOUNT</div>
                            </div>
                            <!-- 登录表单 -->
                            <el-form ref="loginFormRef" :rules="loginRules" :model="loginForm" 
                            label-width="auto" style="max-width: 600px">
                                <el-form-item prop="username">
                                    <el-input size="large" v-model="loginForm.username" placeholder="请输入用户名"
                                    @keyup.enter="loginSubmit(loginFormRef)"/>
                                </el-form-item>
                                <el-form-item prop="password">
                                    <el-input size="large" type="password" v-model="loginForm.password" placeholder="请输入密码"
                                    @keyup.enter="loginSubmit(loginFormRef)"/>
                                </el-form-item>
                                <el-form-item>
                                    <el-button size="large" type="success" @click="loginSubmit(loginFormRef)">登 录</el-button>
                                </el-form-item>
                            </el-form>
                        </div>
                    </div>
                </div>
            </Transition>
        </div>
        <!-- 登录页脚 -->
        <footer class="footer">
            <div>
                <span>版权所有 : 兰州大方电子有限责任公司</span>
                <span>地址：甘肃省兰州市七里河区南滨河中路1500号三维商城电子商务产业孵化大厦九层</span>
            </div>
        </footer>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
//引入api接口
import { login } from '@/api/apis/user.ts';
//引入工具函数
import { getCurrentDate } from '@/utils/tools.ts';
//路由
const router = useRouter();
//引入Pinia
import useUserStore from '@/store/user.ts';
const userStore = useUserStore();
// 定义接口规定属性
interface LoginForm {
    username: string,
    password: string
}
//当前时间
let currentTime = ref('');
let timer:any;

const loginFormRef = ref<FormInstance>();
//登录表单
const loginForm = reactive<LoginForm>({
    username: '',
    password: '',
});
//登录表单验证
const loginRules = reactive<FormRules<LoginForm>>({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
    ]
});

//登录提交
const loginSubmit = async (formEl: FormInstance | undefined) => {
    if(!formEl) return;
    await formEl.validate((valid) => {
        if (valid) {
            //表单验证通过提交表单
            handleLogin()
        } else {
            console.log('error submit!!')
            return false
        }
    })
}

//调用接口
const handleLogin = () => {
    login({
        login_name: loginForm.username,
        password: loginForm.password,
    }).then((res: any) => {
        if(res.code == 200) {
            let userInfo = res.data.length > 0 ? res.data[0] : {};
            //存储用户信息
            userStore.saveUserInfo(userInfo);
            //跳转到主页面
            router.push('/homePage');
        }
    });
}

onMounted(() => {
    currentTime.value = getCurrentDate(true, true, true);
    timer = setInterval(() => {
        currentTime.value = getCurrentDate(true, true, true);
    }, 1000);
});

onUnmounted(() => {
    timer && clearInterval(timer);
});
</script>
<style lang="scss" scoped>
.login {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    width: 100%;
    height: 100%;
    background-image: url('~@/assets/images/imgs/pic-bj.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    .header {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        width: 100%;
        height: 70px;
        background: rgb(255 255 255 / 80%);
        padding: 0 30px;
        .sys-time {
            font-size: 16px;
            font-weight: 600;
            color: #666;
        }
    }
    .container {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 100%;
        flex: 1;
    }
    .footer {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 60px;
        background: rgba(43, 48, 70, 0.8);
        color: #ccc;
        font-size: 14px;
        span {
            margin-right: 20px;
        }
    }
}
.logo {
    width: auto;
    height: 100%;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    .sys-logo {
        margin-top: 5px;
    }
    .sys-name {
        font-family: 'SIMHEI';
        font-size: 36px;
        margin-left: $margin-sm;
        letter-spacing: 5px;
        background: linear-gradient(to right, #0059ff, #9900ff);
        background-clip: text;/*将设置的背景颜色限制在文字中*/
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;/*给文字设置成透明*/
    }
}
.sys-logo > .svg-icons :deep(svg)  {
    width: 40px;
    height: 40px;
}

.login-contain {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    width: 40%;
    height: 70%;
    > div {
        width: 50%;
        height: 100%;
    }
    .contain-left {
        position: relative;
        .left-box {
            position: absolute;
            top: 0;
            left: 0;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: flex-start;
            width: 100%;
            height: 100%;
            background: rgba(2, 8, 13, 0.7);
            .left-text {
                width: 100%;
                font-size: 20px;
                text-align: center;
                margin-bottom: 20px;
                letter-spacing: 2px;
                color: #ababab;
            }
            .left-system {
                width: 100%;
                font-size: 30px;
                text-align: center;
                letter-spacing: 5px;
                color: #5a90b1;
            }
        }
    }
    .contain-right {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        background: #fff;
        .login-box {
            width: 75%;
            height: 50%;
        }
    }
}
.login-box {
    .login-title {
        width: 100%;
        height: 40px;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        margin-bottom: 20px;
        .login-name {
            font-size: 16px;
            font-weight: 500;
            margin-left: 10px;
        }
    }
    .el-form-item {
        margin-bottom: 30px;
    }
    .el-button {
        width: 100%;
        border-radius: 0;
    }
}
.login-title > .svg-icons :deep(svg)  {
    width: 24px;
    height: 24px;
}
.login-title > .login-vite {
    height: 24px;
}
.login-box :deep(.el-input__wrapper) {
    border-radius: 0;
}
</style>